/**
 * @desc: the Datasource Configulation class.via it,you can get 
 *         the Datasource's information,Inc The Datasource's name
 *         Login Database's username,password,and the JDBC's driver,
 *         url and so on.
 * @author: cxlh
 */
package com.ids.transfer.config;

import java.sql.Connection;
import java.sql.SQLTimeoutException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ids.transfer.data.XmlParse;
import com.ids.transfer.util.StrCharUtil;

/**
 * the Datasource Configulation class.via it,you can get the Datasource's
 * information,Inc The Datasource's name Login Database's username,password,and
 * the JDBC's driver, url and so on.
 */
public class DatasourceConfig {
	private Log logger = LogFactory.getLog(getClass());
	
	protected HashMap properties = new HashMap();
	private StringBuffer sb = new StringBuffer();
	
	private DatasourceConfig() {
	}

	/**
	 * 得到属性信息
	 * 
	 * @return
	 */
	public Map getProperties() {
		return (this.properties);
	}

	/**
	 * 构造函数
	 * 
	 * @param dsname
	 *            数据源名称
	 */
	public DatasourceConfig(String dsname) {
		try {
			this.properties = XmlParse.getDataSourceInfo(Constant.XMLFILEPATH,
					dsname);
			if(!check()){
				logger.error("xml解释出错,错误原因"+sb.toString());
				System.exit(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检测合法性
	 * 
	 * @return
	 */
	private boolean check() // 校验TC创建中是否合法
	{
		// 检查必要的信息是否都已设定
		boolean flag = true;
		if (this.getName().equals("")) {
			sb.append("名称必须设定");
			return false;
		}
		if (StrCharUtil.isNull(getConnectionDriver())) {
			sb.append("ConnectionDriver属性必须设定");
			return false;
		}
		if (StrCharUtil.isNull(getPassword())) {
			sb.append("Password属性必须设定");
			return false;
		}
		if (StrCharUtil.isNull(getUrl())) {
			sb.append("Url属性必须设定");
			return false;
		}
		if (StrCharUtil.isNull(getUsername())) {
			sb.append("Username属性必须设定");
			return false;
		}
		return flag;
	}
	
	/**
	 * 得到JDBC连接的数据库密码
	 * 
	 * @return String
	 */
	public String getPassword() {
		String pass = (String) this.properties.get("password");
		return pass;
	}

	/**
	 * JDBC驱动
	 * 
	 * @return
	 */
	public String getConnectionDriver() {
		String str = (String) this.properties.get("connection-driver");
		return str;
	}

	/**
	 * 用户名
	 * 
	 * @return
	 */
	public String getUsername() {
		String pass = (String) this.properties.get("username");
		return pass;
	}

	/**
	 * JDBC的URL连接
	 * 
	 * @return
	 */
	public String getUrl() {
		String str = (String) this.properties.get("url");
		return str;
	}

	public String getEndpoint() {
		String str = (String) this.properties.get("endpoint");
		return str;
	}

	/**
	 * 数据库类型
	 * 
	 * @return
	 */
	public String getDbtype() {
		String str = (String) this.properties.get("dbtype");
		return str;
	}

	public int getNumPerPage() {
		String str = this.getDbtype();
		// 解析一页的记录数
		int endIdx = str.lastIndexOf(":");
		str = str.substring(endIdx + 1, str.length());
		int res = 0;
		try {
			res = Integer.parseInt(str);
		} catch (Exception ex) {
			res = 100;
		}
		return res;
	}

	/**
	 * 数据源名称
	 * 
	 * @return
	 */
	public String getName() {
		String str = (String) this.properties.get("name");
		return str;
	}

	/**
	 * 获得连接
	 * 
	 * @return JDBC Connection
	 * @throws SQLTimeoutException 
	 */
	public Connection getConnection() throws SQLTimeoutException {
		try {
			logger.info("connection to the server...");
			long startTime = System.currentTimeMillis();
			Connection conn = XmlParse.getConn(properties);
			logger.info("connection success! consume "+
					(System.currentTimeMillis()-startTime)+" million second");
			return conn;
		} catch (Exception ex) {
			logger.error("connection failure!",ex);
			throw new SQLTimeoutException();
		}
	}

	/**
	 * 重载基类的toString函数
	 * 
	 * @return
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer("DATASOURCEINFO[");
		Iterator names = properties.keySet().iterator();
		while (names.hasNext()) {
			String name = (String) names.next();
			String value = properties.get(name).toString();
			sb.append(name);
			sb.append('=');
			sb.append(value);
			sb.append(',');
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("]");
		return (sb.toString());
	}

	public static void main(String[] args) {
		try {
			String str = "WP:ORACLE:100";
			str = str.substring(str.lastIndexOf(":") + 1, str.length());
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}